/**
 * Created by Administrator on 2014/12/24.
 */

package com.smartrobot.smartrobot.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.smartrobot.smartrobot.Common.Common;
import com.smartrobot.smartrobot.R;
import com.smartrobot.smartrobot.activity.ChatActivity;
import com.smartrobot.smartrobot.activity.ChatActivity1;
import com.smartrobot.smartrobot.logToSdCard.LogToSd;
import com.smartrobot.smartrobot.pojo.getFriendListPojo;
import com.smartrobot.smartrobot.util.ImageLoaderApi;


import java.util.ArrayList;
import java.util.List;


public class FriendViewAdapter extends BaseAdapter implements AdapterView.OnItemClickListener{

    private Context context;
    private List<getFriendListPojo> list;
    private ImageLoader imageLoader;
//    private AdapterView<?> adapterView;
//    private View view;
//    private int position;
//    private long id;

    public FriendViewAdapter(Context context,List<getFriendListPojo> activeList){

        this.context = context;
        this.list = new ArrayList<getFriendListPojo>();
        this.list.addAll(activeList);//将所有list添加进来
        imageLoader = ImageLoader.getInstance();
//        imageLoader.init(ImageLoaderApi.getImageLoaderConfig(context));

    }



    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        try{
            ViewHolder holder;
            if(view == null){
                //新建一个holder
                view =  LayoutInflater.from(context).inflate(R.layout.page_social_circle_adapter_item, viewGroup, false);
                holder = new ViewHolder();

                holder.headImg = (ImageView) view.findViewById(R.id.social_circle_adapter_item_headImg);
                holder.nickName = (TextView)view.findViewById(R.id.social_circle_adapter_item_nickname);
                holder.sex = (ImageView)view.findViewById(R.id.social_circle_adapter_item_sex);
                holder.sign = (TextView)view.findViewById(R.id.social_circle_adapter_item_sign);
                holder.item = (RelativeLayout)view.findViewById(R.id.circle_friend_adapter);
                view.setTag(holder);

            }else{
                holder = (ViewHolder)view.getTag();
            }

            //初始化所有view
            String showName = list.get(position).getUserAccount();
            String nickName =list.get(position).getName();
            String reName = list.get(position).getReName();


            if(nickName!=null&&!nickName.equals("")){
                showName = nickName;
            }else if(reName!=null&&!reName.equals("")){
                showName = reName;
            }

            holder.nickName.setText(showName);
            holder.sign.setText(list.get(position).getSign());

            imageLoader.displayImage(Common.baseUrl+list.get(position).getHeadImg(),holder.headImg, ImageLoaderApi.getOptions());


            /*性别*/
            holder.sex.setVisibility(View.VISIBLE);
            if(list.get(position).getSex().equals("男")){
                holder.sex.setBackgroundResource(R.drawable.men_icon);
            }else if(list.get(position).getSex().equals("女")){
                holder.sex.setBackgroundResource(R.drawable.woman_icon);
            }else{
                holder.sex.setVisibility(View.GONE);
            }


        }catch (Exception e){
            LogToSd.error(e.getMessage());
        }
        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
//        Bundle bundle = new Bundle();//该类用作携带数据
//        bundle.putSerializable("user_msg",list.get(position));
//        bundle.putInt("flag",1);
        Intent intent = new Intent();
        intent.setClass(context, ChatActivity1.class);

        intent.putExtra("userId", list.get(position).getUserAccount());
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);

    }


    class ViewHolder{
        RelativeLayout item;
        ImageView headImg;
        TextView nickName;
        ImageView sex;
        TextView sign;
    }

    public  void  notifyChange(List<getFriendListPojo> list,ListView listView){

        ViewGroup.LayoutParams params = listView.getLayoutParams();
//        params.height = evr_item_height*(list.size()+1);
        params.height = (Dp2Px()+ listView.getDividerHeight())*list.size() ;

        this.list.clear();
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

    public void notifyFriendChange(List<getFriendListPojo> list){

        Log.e("notifyFriendChange!!!!!!!!!!!!!!!!!!!!!!!",list.get(0).toString());
        this.list.clear();
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

    public int Dp2Px() {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (62 * scale + 0.5f);
    }

}
